﻿// Copyright (C) 2009 - 2013 EvTech Project
//
// This program is free software: you can redistribute it and/or modify
// it under the +terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/. 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Piirto
{
    public partial class SelectImageForm : Form
    {
        private List<Image> images;
        private int currentPage;
        private Image selectedImage;
        private List<String> folders;

        public SelectImageForm(String path)
        {
            InitializeComponent();
            currentPage = 0;
            this.folders = new List<String>();
            this.images = new List<Image>();
            for (int i = 0; i < 6; i++)
            {
                PictureBox pbox = new PictureBox();
                pbox.SizeMode = PictureBoxSizeMode.Zoom;
                pbox.Image = null;
                pbox.MouseClick += new MouseEventHandler(pictureBox_Click);
                this.tableLayoutPanel1.Controls.Add(pbox);
                pbox.Dock = DockStyle.Fill;
            }
            try
            {
                string[] fileNames = Directory.GetFiles(path);
                folders.Add(path);
                foreach (string filename in fileNames)
                {
                    try
                    {
                        Image img = Image.FromFile(filename);
                        this.images.Add(img);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.StackTrace);
            }
            if (this.images.Count > 0)
            {
                selectedImage = this.images.ElementAt(0);
            }
            setImages();
        }

        public void addImageFolder(String path)
        {
            try
            {
                string[] fileNames = Directory.GetFiles(path);
                folders.Add(path);
                foreach (string filename in fileNames)
                {
                    try
                    {
                        Image img = Image.FromFile(filename);
                        this.images.Add(img);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.StackTrace);
            }
            setImages();
        }

        public void refreshImages()
        {
            this.images.Clear();
            currentPage = 0;
            foreach (string path in folders)
            {
                try
                {
                    string[] fileNames = Directory.GetFiles(path);
                    foreach (string filename in fileNames)
                    {
                        try
                        {
                            Image img = Image.FromFile(filename);
                            this.images.Add(img);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.Out.WriteLine(e.StackTrace);
                }
            }
            if (this.images.Count > 0)
            {
                selectedImage = this.images.ElementAt(0);
            }
            setImages();
        }
        private void setImages()
        {
            for (int i = 0; i < this.tableLayoutPanel1.Controls.Count; i++)
            {
                if (currentPage * 6 + i < images.Count)
                {
                    ((PictureBox)this.tableLayoutPanel1.Controls[i]).Image = images.ElementAt(currentPage * 6 + i);
                }
                else
                {
                    ((PictureBox)this.tableLayoutPanel1.Controls[i]).Image = null;
                }
            }
            if (currentPage > 0)
            {
                btnPrev.Visible = true;
            }
            else
            {
                btnPrev.Visible = false;
            }
            if ((currentPage + 1) * 6 < images.Count)
            {
                btnNext.Visible = true;
            }
            else
            {
                btnNext.Visible = false;
            }
        }

        private void pictureBox_Click(object sender, EventArgs e)
        {
            if(((PictureBox)sender).Image!=null)
            {
                selectedImage = ((PictureBox)sender).Image;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            currentPage--;
            setImages();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            currentPage++;
            setImages();
        }

        internal Image getSelectedImage()
        {
            return selectedImage;
        }

        private void SelectImageForm_Resize(object sender, EventArgs e)
        {
            int scaleX = this.Width / 100;
            int scaleY = this.Height / 100;
            tableLayoutPanel1.SetBounds(5 * scaleX, 5 * scaleY, 90 * scaleX, 70 * scaleY);
            btnPrev.SetBounds(5 * scaleX, 80 * scaleY, 25 * scaleX, 15 * scaleY);
            btnNext.SetBounds(70 * scaleX, 80 * scaleY, 25 * scaleX, 15 * scaleY);
            btnCancel.SetBounds((int)(37.5 * scaleX), 80 * scaleY, 25 * scaleX, 15 * scaleY);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}
